<?php


namespace app\api\controller;

use app\admin\model\finance\extract\Week;
use app\admin\model\Management;
use app\common\controller\Api;
use think\Db;
use livez\Livez;
use app\common\model\User;
class Live extends Api
{

	public function _initialize(){
		$this->Livez = new Livez();
        parent::_initialize();
		//获取默认值
	}
	/**
     * 点击开播
	 * @author gzy
     */
	public function BeginShow(){
		$userid = $this->request->request('userid');
        if (!$userid)
        {
            $this->error(__('Invalid parameters'));
        }
        $info = User::get($userid);
        if (!$info) $this->error('用户不存在');
        if($info->role_publisher != 1 && !$info->channelname) $this->error('此用户不能直播');
    	$result = $info->save(['show'=>2]);
    	$this->success('操作成功');
      
	}	
	/**
     * 关闭直播
	 * @author gzy
     */
	public function CloseShow(){
		$userid = $this->request->request('userid');
        if (!$userid)
        {
            $this->error(__('Invalid parameters'));
        }
        $info = User::get($userid);
        if (!$info) $this->error('用户不存在');
        if($info->role_publisher != 1 && !$info->channelname) $this->error('此用户不能直播');
    	$result = $info->save(['show'=>1]);
    	$this->success('操作成功');
      
	}
 	/**
     * 获取tokey
	 * @author gzy
     */
    public function RtcTokenBuilder(){
    	$userid = $this->request->request('userid');
    	$channelname = $this->request->request('channelName');
    	$role = $this->request->request('role');
        if (!$userid || !$channelname || !$role) $this->error(__('Invalid parameters'));
        $info = User::get($userid);
        if (!$info) $this->error('用户不存在');
        $this->Livez->uid = $userid;
     
        $User = TableField('LiveUser','id',$channelname,'channelname');
        if(!$User) $this->error("频道不存在");
        $this->Livez->channelname = $channelname;
        $this->Livez->role = $role;
        if($role == 1 && $info->role_publisher == 2) $this->error("你不能开播哦");
    	if($role == 2 && $User == $userid) $this->error("不能看自己的哦");
        $tokey = $this->Livez->RtcTokenBuilder();
       	 if($tokey){
        	$data = ['tokey' => $tokey];
        	$this->success('获取成功', $data);
        }
    }
    /**
     * 获取tokey
	 * @author gzy
     */
    public function RtmTokenBuilder(){
    	$userid = $this->request->request('userid');
    	$role = $this->request->request('role');
    	if (!$userid  || !$role)$this->error(__('Invalid parameters'));
        $info = User::get($userid);
        if (!$info) $this->error('用户不存在');
        $this->Livez->uid = $userid;
        $this->Livez->role = $role;
        $tokey = $this->Livez->RtmTokenBuilder();
       	 if($tokey){
        	$data = ['tokey' => $tokey];
        	$this->success('获取成功', $data);
        }
    }  
   	/**
     * 频道列表
	 * @author gzy
     */
    public function ChannelList(){
       $list = User::where(['show'=>2,'role_publisher'=>1])->field("id,username,nickname,channelname")->select();
       if(!$list)  $this->error('数据为空');
    	$list = collection($list)->toArray();
   		$this->success('获取成功', $list);
      
    }  
  
   
}